package com.ljh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.ljh.common.JsonData;
import com.ljh.mapper.BilltableMapper;
import com.ljh.pojo.Billdefail;
import com.ljh.pojo.Billtable;
import com.ljh.pojo.Menutable;
import com.ljh.mapper.MenutableMapper;
import com.ljh.service.MENUTABLEervice;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljh.utils.ExceptionUtils;
import com.ljh.utils.StringUtils;
import com.ljh.vo.BillDefaiVo;
import com.ljh.vo.MenutableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘剑鸿
 * @since 2021-11-02
 */
@Service
public class MenutableServiceImpl extends ServiceImpl<MenutableMapper, Menutable> implements MENUTABLEervice {

    @Autowired
    private MenutableMapper menutableMapper;

    @Autowired
    private BilltableMapper billtableMapper;

    public JsonData menuAll(MenutableVo menutableVo){
        Page<Menutable> page = PageHelper.startPage(menutableVo.getPage(),menutableVo.getLimit());
        List<Menutable> men = menutableMapper.selectMenuOne(menutableVo);

        if (CollectionUtils.isEmpty(men)){
            men = Lists.newArrayList();
        }
        return JsonData.success(page.getTotal(),men);
    }
    public List<Menutable> ToBill(BillDefaiVo billDefaiVo){
       Integer[] ids = StringUtils.stringIdsToInt(billDefaiVo.getIds());

           List<Menutable> Menu =  menutableMapper.selectBatchIds(Arrays.asList(ids));
        //返回循环选中菜单ID
            System.out.println(Menu);
        return Menu;
    }

    public void OnBill(BillDefaiVo billDefaiVo, Billtable billtable){
        Integer tableId = billDefaiVo.getOutTableid();
        String[] getcount = billDefaiVo.getConuts();
        if (tableId > 0 && tableId !=null && getcount != null){
            Integer[] Menuid = StringUtils.stringIdsToInt(billDefaiVo.getMids());
            Integer[] count = StringUtils.stringIdsToInt(getcount);
            //新增主账单
            StringBuilder billnumber = StringUtils.Stringrandom();
            String s = billnumber.toString();
            billtable.setBillnumber(s);
            billtable.setId(billtable.getId());
            billtable.setDeleted(0);
            int num = billtableMapper.insertbill(billtable);
            int billid = billtable.getBillid();
            System.out.println("新增"+billid);
            if (num > 0){
                //billDefaiVo.setBillid(num);
                for (int i = 0; i<Menuid.length;i++){
                    for (int j = 0;j<count.length;j++){
                        if (i == j){
                            menutableMapper.AppBill(Menuid[i],tableId,count[j],billid);
                        }
                    }
                }
            }
            else{
                throw new ExceptionUtils("新增账单失败");
            }
        }
        else{
            throw new ExceptionUtils("请选择具体餐桌");
        }

    }


}
